﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Data.Repository;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Util.Model;
using YiSha.Util;
using YiSha.Enums;

namespace YiSha.Service.OrganizationManage
{
    /// <summary>
    /// 职位服务
    /// </summary>
    public class PositionService : RepositoryFactory
    {

        #region 获取数据
        public TData<List<PositionEntity>> GetList(PositionListParam param)
        {
            TData<List<PositionEntity>> obj = new TData<List<PositionEntity>>();
            var expression = ExtLinq.True<PositionEntity>();
            if (!param.PositionName.IsEmpty())
            {
                expression = expression.And(a => a.PositionName.Contains(param.PositionName));
            }
            if (!param.PositionIds.IsEmpty())
            {
                var idArr = param.PositionIds.Split(",");
                expression = expression.And(a => idArr.Contains(a.Id));
            }
            obj.Data = SqlSugarAdapters.GetList<PositionEntity>(expression);
            return obj;
        }

        public TData<List<PositionEntity>> GetPageList(PositionListParam param, Pagination pagination)
        {
            TData<List<PositionEntity>> obj = new TData<List<PositionEntity>>();
            var expression = ExtLinq.True<PositionEntity>();
            if (!param.PositionName.IsEmpty())
            {
                expression = expression.And(a => a.PositionName.Contains(param.PositionName));
            }
            if (!param.PositionIds.IsEmpty())
            {
                var idArr = param.PositionIds.Split(",");
                expression = expression.And(a => idArr.Contains(a.Id));
            }
            var list = SqlSugarAdapters.GetPager(expression, pagination);
            obj.Data = list.Item2;
            obj.Total = list.Item1;
            return obj;
        }

        public TData<PositionEntity> GetEntity(string id)
        {
            TData<PositionEntity> obj = new TData<PositionEntity>();
            obj.Data = SqlSugarAdapters.GetEntity<PositionEntity>(s => s.Id == id);
            return obj;
        }

        #endregion

        #region 提交数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public TData SaveForm(PositionEntity entity)
        {
            TData obj = new TData();
            try
            {
                if (ExistMenuName(entity))
                {
                    obj.MsgCode = MsgCode.Fail;
                    obj.Msg = "职位名称已经存在！";
                    return obj;
                }
                if (entity.Id.IsEmpty())
                {
                    _ = entity.Create();
                    this.SqlSugarAdapters.Insert(entity);
                }
                else
                {
                    _ = entity.Modify();
                    this.SqlSugarAdapters.Update(entity);
                }
                return obj;
            }
            catch (Exception ex)
            {
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = ex.Message;
                return obj;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public TData DeleteForm(List<string> ids)
        {
            TData obj = new TData();
            try
            {
                var info = SqlSugarAdapters.GetList<PositionEntity>(s => ids.Contains(s.Id));
                if (info.Count > 0)
                {
                    var id = info.Select(s => s.Id).ToList();
                    SqlSugarAdapters.Delete<PositionEntity>(s => id.Contains(s.Id));
                    return obj;
                }
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = "当前数据不存在";
                return obj;
            }
            catch (Exception ex)
            {
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = ex.Message;
                return obj;
            }
        }
        private bool ExistMenuName(PositionEntity entity)
        {
            var expression = ExtLinq.True<PositionEntity>();
            expression = expression.And(t => t.Valid);
            if (entity.Id.IsEmpty())
            {
                expression = expression.And(t => t.PositionName == entity.PositionName);
            }
            else
            {
                expression = expression.And(t => t.PositionName == entity.PositionName && t.Id != entity.Id);
            }
            return SqlSugarAdapters.GetList(expression).Count() > 0 ? true : false;
        }
        #endregion
    }
}
